/**
 *
 * Bend shader
 * http://www.everyday3d.com/j3d/demo/013_Paper.html
 */

THREE.BendShader = {

    uniforms: {

        "bendAngle": { type: "f", value: 0 },
        "bounds": { type: "v2", value: new THREE.Vector2(-100, 100) },
        "bendOffset": { type: "f", value: 0 },
        "bendAxisAngle": { type: "f", value: 0 },
        "colorTexture": {type: "t", value: null}
    },

    vertexShader: [
	"varying vec2 vUv;",
	
    "uniform float bendAngle;",
	"uniform vec2 bounds;",
	"uniform float bendOffset;",
	"uniform float bendAxisAngle;",
	
	"vec3 bend(vec3 ip, float ba, vec2 b, float o, float a) {",
	"vec3 op = ip;",
	"ip.x = op.x * cos(a) - op.y * sin(a);",
	"ip.y = op.x * sin(a) + op.y * cos(a);",
	"if(ba != 0.0) {",
		"float radius = b.y / ba;",
		"float onp = (ip.x - b.x) / b.y - o;",
		"ip.z = cos(onp * ba) * radius - radius;",
		"ip.x = (b.x + b.y * o) + sin(onp * ba) * radius;",
	"}",
	"op = ip;",
	"ip.x = op.x * cos(-a) - op.y * sin(-a);",
	"ip.y = op.x * sin(-a) + op.y * cos(-a);",
	"return ip;",
	"}",

	"void main(void) {",
        "vUv = uv;",
	    "//vNormal = normalize( ( modelViewMatrix * vec4( normal, 0.0 ) ).xyz );",
		"vec3 p = bend(position, bendAxisAngle, bounds, bendOffset, bendAngle);",
	    "gl_Position = projectionMatrix * modelViewMatrix * vec4( p, 1.0 );",
	"}"

	].join("\n"),

    fragmentShader: [
	"varying vec2 vUv;",
	"uniform sampler2D colorTexture;",
	
	"void main(void) {",
		"vec2 position = vUv;",
		"gl_FragColor = texture2D(colorTexture, position);",
	"}"

	].join("\n")

};
